package org.lucava.boatskeeper.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.lucava.boatskeeper.client.BoatsKeeperService;
import org.lucava.boatskeeper.shared.Boat;
import org.lucava.boatskeeper.shared.User;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class BoatsKeeperServiceImpl extends RemoteServiceServlet implements
		BoatsKeeperService {

	private static final long serialVersionUID = -5695352561576272320L;

	private Map<Integer, Boat> mBoats = new HashMap<Integer, Boat>();

	private Map<Integer, User> mUsers = new HashMap<Integer, User>();

	public BoatsKeeperServiceImpl() {
		Boat b = new Boat();
		b.setName("firstBoat");
		b.setId(1);
		mBoats.put(b.getId(), b);

		Boat b2 = new Boat();
		b2.setName("secondBoat");
		b2.setId(2);
		mBoats.put(b2.getId(), b2);

		Boat b3 = new Boat();
		b3.setName("thirdBoat");
		b3.setId(3);
		mBoats.put(b.getId(), b3);

		Boat b4 = new Boat();
		b4.setName("fourthBoat");
		b4.setId(4);
		mBoats.put(b4.getId(), b4);

		User u = new User();
		u.setName("firstUser");
		u.setId(1);
		mUsers.put(u.getId(), u);

		User u2 = new User();
		u2.setName("secondUser");
		u2.setId(2);
		mUsers.put(u2.getId(), u2);
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html
	 *            the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	@Override
	public List<Boat> getBoats() {
		return new ArrayList<Boat>(mBoats.values());
	}

	@Override
	public Boolean deleteBoat(Integer id) {
		boolean result = this.mBoats.containsKey(id);
		this.mBoats.remove(id);
		return result;
	}

	@Override
	public List<Boat> deleteBoats(List<Integer> ids) {

		Iterator<Integer> idIt = ids.iterator();
		while (idIt.hasNext()) {
			this.deleteBoat(idIt.next());
		}

		return this.getBoats();
	}

	@Override
	public Boat getBoat(Integer id) {
		Boat boat = this.mBoats.get(id);

		if (boat == null) {
			boat = new Boat();
			boat.setName("ERROR");
			boat.setType("'" + id + "'");
			boat.setDescription("desc");
		}
		return boat;
	}

	@Override
	public Boat updateBoat(Boat aBoat) {
		this.mBoats.put(aBoat.getId(), aBoat);
		return aBoat;
	}

	@Override
	public Boat addBoat(Boat aBoat) {
		this.mBoats.put(aBoat.getId(), aBoat);
		return aBoat;
	}

	@Override
	public List<User> getUsers() {
		return new ArrayList<User>(mUsers.values());
	}

	@Override
	public Boolean deleteUser(Integer id) {
		boolean result = this.mUsers.containsKey(id);
		this.mUsers.remove(id);
		return result;
	}

	@Override
	public List<User> deleteUsers(List<Integer> ids) {

		Iterator<Integer> idIt = ids.iterator();
		while (idIt.hasNext()) {
			this.deleteUser(idIt.next());
		}

		return this.getUsers();
	}

	@Override
	public User getUser(Integer id) {
		User boat = this.mUsers.get(id);

		if (boat == null) {
			boat = new User();
			boat.setName("ERROR");
		}
		return boat;
	}

	@Override
	public User updateUser(User aUser) {
		this.mUsers.put(aUser.getId(), aUser);
		return aUser;
	}

	@Override
	public User addUser(User aUser) {
		this.mUsers.put(aUser.getId(), aUser);
		return aUser;
	}
}
